import { createSlice } from '@reduxjs/toolkit'

const shoppingCartStore = createSlice({
  // 模块名称独一无二
  name: 'cart',
  // 初始数据
  initialState: {
    count: 1,
  },
  // 修改数据的同步方法
  reducers: {
    increment(state) {
      state.count++
    },
    decrement(state) {
      state.count--
    },
    add(state, actions) {
      state.count += actions.payload
    },
  },
})
// 结构出actionCreater
const { increment, decrement, add } = shoppingCartStore.actions

// 获取reducer函数
const cartReducer = shoppingCartStore.reducer

// 导出
export { increment, decrement, add }
export default cartReducer
